import {createSlice,PayloadAction,createAsyncThunk} from '@reduxjs/toolkit'
import axios from 'axios'
interface LoginInState{
    isloading:boolean,
    error:string|null,
    token:string|null,
    temp:boolean
}
const initialState:LoginInState={
    isloading:false,
    error:null,
    token:null,
    temp:true
}
interface usertype{
    email:string,
    password:string
}
export const getLoginUser=createAsyncThunk(
    "Login/LoginUser",
    async (userLoginParams:{email:string,password:string})=>{
        const {data} = await axios.post('http://123.56.149.216:8080/auth/login',{
            email:userLoginParams.email,
            password:userLoginParams.password
        },{headers: {"x-icode": "69ACF3816BF5D32D"}})
        return data.token;
    }
)
export const LoginSlice=createSlice({
    name:'Login',
    initialState,
    reducers:{
        loginout:(state)=>{
        state.token=null;
        state.error=null;
        state.isloading=false;
        state.temp=true;
        }
    },
    extraReducers:{
        [getLoginUser.pending.type]:(state)=>{
            state.isloading=true;
        },
        [getLoginUser.fulfilled.type]:(state,action)=>{
            state.isloading=false;
            state.token=action.payload;
        },
        [getLoginUser.rejected.type]:(state,action)=>{
            state.isloading=false;
            state.error=action.error.message;
            state.temp=!state.temp;
        }
    }
})